package com.sh.data.engine.application.model.dto.integration;

import com.sh.data.engine.application.model.request.integration.PreviewExpressRequest;
import com.sh.data.engine.application.model.request.integration.TestApiHttpRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/27 14:14
 */
@Data
@Builder
@Schema(description = "保存任务请求")
@NoArgsConstructor
@AllArgsConstructor
public class HttpTaskDTO {

    @Schema(description = "任务id", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long id;

    @Schema(description = "任务名称", requiredMode = Schema.RequiredMode.REQUIRED)
    private String taskName;

    // api相关
    @Schema(description = "api参数", requiredMode = Schema.RequiredMode.REQUIRED)
    private TestApiHttpRequest apiHttpRequest;

    @Schema(description = "api返回值配置", requiredMode = Schema.RequiredMode.REQUIRED)
    private PreviewExpressRequest previewExpressRequest;

    // 任务配置相关
    @Schema(description = "字段配置,表配置", requiredMode = Schema.RequiredMode.REQUIRED)
    private TaskConfigRequest taskConfigRequest;

    @Data
    @Schema(description = "任务关联数据源,调度参数")
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TaskConfigRequest {
        // 目标数据源
        @Schema(description = "目标数据源id", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long targetDsId;

        // 目标数据源类型
        @Schema(description = "目标数据源类型", requiredMode = Schema.RequiredMode.REQUIRED)
        private String targetDsType;
        // 目标表
        @Schema(description = "目标表名", requiredMode = Schema.RequiredMode.REQUIRED)
        private String targetTableName;

        // 替换策略
        @Schema(description = "主键替换策略")
        private Integer dataReplaceRuleId;

        // 同步前清空历史记录
        @Schema(description = "是否在同步前清空记录", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer isTruncate;

        // 失败记录阈值
        @Schema(description = "失败次数阈值", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer errorNumLimit;

        @Schema(description = "字段映射方式", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer fieldMapType;

        // 字段映射
        @Schema(description = "普通字段列表", requiredMode = Schema.RequiredMode.REQUIRED)
        private List<FieldMapping> fieldMappings;

        @Schema(description = "mongo hbase", requiredMode = Schema.RequiredMode.REQUIRED)
        private List<TargetField> targetFields;

        @Schema(description = "分区字段列表")
        private List<PartitionField> partitions;

        // 同步配置
        // 生效时间
        /*调度状态 0-未冻结 1-冻结"*/
        @Schema(description = "任务冻结状态", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer status;

        /*生效开始时间*/
        @Schema(description = "生效开始日期", requiredMode = Schema.RequiredMode.REQUIRED)
        private Date effectiveDateStart;

        /*生效结束时间*/
        @Schema(description = "生效结束日期", requiredMode = Schema.RequiredMode.REQUIRED)
        private Date effectiveDateEnd;

        /*调度周期 0-月 1-周 2-天 3-小时 4-分钟*/
        @Schema(description = "调度周期 0-月 1-周 2-天 3-小时 4-分钟", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer cycle;

        /*执行开始时间的小时*/
        @Schema(description = "执行开始时间的小时", requiredMode = Schema.RequiredMode.REQUIRED)
        private String startTimeHour;

        /*执行开始时间的分钟*/
        @Schema(description = "执行开始时间的分钟", requiredMode = Schema.RequiredMode.REQUIRED)
        private String startTimeMin;

        /*执行结束时间的小时*/
        @Schema(description = "执行结束时间的小时", requiredMode = Schema.RequiredMode.REQUIRED)
        private String endTimeHour;

        /*执行结束时间的分钟*/
        @Schema(description = "执行结束时间的分钟", requiredMode = Schema.RequiredMode.REQUIRED)
        private String endTimeMin;

        /*
         * 月、周为执行日期 ，小时、分钟为执行间隔
         */
        @Schema(description = "执行时间", requiredMode = Schema.RequiredMode.REQUIRED)
        private String execPoint;

        /*跨周期依赖 0-不依赖上一周期 1-等待上一周期结束*/
        @Schema(description = "执行时间", requiredMode = Schema.RequiredMode.REQUIRED)
        private Integer cycleDependence;

        @Data
        @Schema(description = "普通字段映射")
        @NoArgsConstructor
        @AllArgsConstructor
        public static class FieldMapping {
            @NotNull
            @Schema(description = "第二步中用户选择表的字段名", requiredMode = Schema.RequiredMode.REQUIRED)
            private String sourceField;

            @NotNull
            @Schema(description = "第二步中用户选择表的字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
            private String sourceFieldType;

            @Schema(description = "该键是否是主键")
            private Integer isSourceFieldPk;

            @NotNull
            @Schema(description = "目标字段名", requiredMode = Schema.RequiredMode.REQUIRED)
            private String targetField;

            @NotNull
            @Schema(description = "目标字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
            private String targetFieldType;

            @Schema(description = "是否是主键")
            private Integer isTargetFieldPk;

            @Schema(description = "目标字段描述")
            private String targetComment;
        }

        @Data
        @Schema(description = "mongo hbase字段映射")
        @NoArgsConstructor
        @AllArgsConstructor
        public static class TargetField {
            @NotNull
            @Schema(description = "第二步中用户选择表的字段名", requiredMode = Schema.RequiredMode.REQUIRED)
            private String fieldName;

            @NotNull
            @Schema(description = "第二步中用户选择表的字段类型", requiredMode = Schema.RequiredMode.REQUIRED)
            private String fieldType;
        }

        @Data
        @Schema(description = "分区字段")
        @NoArgsConstructor
        @AllArgsConstructor
        public static class PartitionField {

            @NotNull
            @Schema(description = "分区字段", requiredMode = Schema.RequiredMode.REQUIRED)
            private String fieldName;

            @NotNull
            @Schema(description = "分区字段值", requiredMode = Schema.RequiredMode.REQUIRED)
            private String fieldValue;
        }
    }
}
